package middleware

import (
	"gitee.com/go-course/go11/devcloud-mini/maudit/apps/log"
	kafka "github.com/segmentio/kafka-go"
)

func NewAuditSendorConfig() *AuditSendorConfig {
	return &AuditSendorConfig{
		Brokers: []string{"localhost:9092"},
		Topic:   log.DEFAULT_OPERATE_LOG_TOPIC,
	}
}

// consumer
// kakfa的配置 有 maudit负责这定义
type AuditSendorConfig struct {
	Brokers []string `toml:"brokers" env:"MAUDIT_KAFKA_BROKERS" envSeparator:","`
	Topic   string   `toml:"topic" env:"MAUDIT_KAFKA_TOPIC"`
}

func (k *AuditSendorConfig) GetWriter() *kafka.Writer {
	w := &kafka.Writer{
		Addr:                   kafka.TCP(k.Brokers...),
		Topic:                  k.Topic,
		Balancer:               &kafka.LeastBytes{},
		AllowAutoTopicCreation: true,
	}
	return w
}
